package cz.spock.cms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cz.spock.cms.entity.Node;
import cz.spock.cms.service.NodeService;
import cz.spock.cms.service.ProjectService;
import cz.spock.core.controller.BaseController;
import cz.spock.exception.InvalidInputException;

/**
 * serves node related requests
 * 
 * TODO refactor, error handling
 * 
 * @author miso
 *
 */
@Controller
public class NodeController extends BaseController {
    
    @Autowired
    private NodeService nService;
    @Autowired
    private ProjectService pService;
    
    // non-ajax
    
    
//    @RequestMapping(value = "/cms/nodes", method = RequestMethod.GET)
//    public String nodes(HttpServletRequest request, HttpServletResponse response) {
//        System.out.println("/cms/nodes");
//        // find all nodes for current user
//        
//        return new ModelAndView("cms/project/projects", "nodes", nl);
//    }
    
    @RequestMapping(value = "/cms/{project}/nodes", method = RequestMethod.GET)
    public ModelAndView projectNodes(@PathVariable Long project, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + project + "/nodes");
        // find all nodes for current project
        // TODO find project by id, then by name... and result use for node search
        List<Node> nl = nService.findNodeByProject(pService.getProjectById(project));
        System.out.println(nl.size());
        return new ModelAndView("cms/node/nodes", "nodes", nl).addObject("project", project);
    }
    
    @RequestMapping(value = "/cms/node/{id}", method = RequestMethod.GET)
    public ModelAndView node(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/node/" + id);
        Node n = nService.getNodeById(id); 
        return new ModelAndView("cms/node/node").addObject("node", n);
    }

    @RequestMapping(value = "/cms/{node}/node/createsub", method = RequestMethod.GET)
    public ModelAndView nodeCreateSubNodeGet(@PathVariable Long node, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + node + "/node/createsub get");
        return new ModelAndView("cms/node/createsub", "node", node);
    }
    
    /**
     * creates sub node, adds relations, redirect to created node
     * 
     * @param node
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/cms/{node}/node/createsub", method = RequestMethod.POST)
    public String nodeCreateSubNodePost(@PathVariable Long node, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + node + "/node/createsub post");
        // create node
        Node child = null;
        try {
            Node parent = nService.getNodeById(node);
            child = nService.createNode(request.getParameter("url"), 
                    ((Long)parent.getProject().getId()).toString(), 
                    request.getParameter("label"));
            nService.createParentChildRelation(parent.getId(), child.getId());
        } catch(Exception e) {            
            sendJson(response, pService.errorToJson(e.getMessage(), 0));
            return null;
//            return;
        }
        return "redirect:/cms/node/" + child.getId().toString();
    }

    
    @RequestMapping(value = "/cms/{project}/node/create", method = RequestMethod.GET)
    public ModelAndView nodeCreateGet(@PathVariable Long project, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + project + "/node/create get");
        return new ModelAndView("cms/node/create", "project", project);
    }
    
    @RequestMapping(value = "/cms/{project}/node/create", method = RequestMethod.POST)
    public String nodeCreatePost(@PathVariable Long project, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/" + project + "/node/create post");
        // create node
        try {
            nService.createNode(request.getParameter("url"), 
                    project.toString(), 
                    request.getParameter("label"));
        } catch(Exception e) {            
            sendJson(response, pService.errorToJson(e.getMessage(), 0));
            return null;
//            return;
        }
        return "redirect:/cms/" + project + "/nodes";
    }
    
    @Deprecated // implementation later
    @RequestMapping(value = "/cms/node/delete", method = RequestMethod.POST)
    public String projectDelete(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("/cms/project/delete");
        // delete project
        
        return "cms/projects";
    }

}
